package Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import Logic.Log;

public class DBConnect {
	
	private Connection con = null;
	private String strCommand = "";
	PreparedStatement ps = null;
	
	public DBConnect(){
		try {
			String url = "sun.jdbc.odbc.JdbcOdbcDriver";
			Class.forName(url);
			String dbUrl = "jdbc:odbc:Driver={SQL Server}; Server=192.168.1.100; Database=quanlynhansu; Integrated Security=True";
			con = DriverManager.getConnection(dbUrl);
		} catch (Exception e) {
			// TODO: handle exception
			Log.Write(e.getMessage());
		}
	}
	
	public ResultSet executeSQLQuery() {
		try {
			ps = con.prepareStatement(this.strCommand);
			ResultSet rs=ps.executeQuery();
			return rs;
		} catch (Exception e) {
			// TODO: handle exception
			Log.Write(e.getMessage());
			return null;
		}
	}

	public String getStrCommand() {
		return strCommand;
	}

	public void setStrCommand(String strCommand) {
		this.strCommand = strCommand;
	}

	public void addParameter(int index, Object param) {
		try {
			if (param instanceof Integer) {
				ps.setInt(index, (Integer)param);
			}
			else if (param instanceof Float) {
				ps.setFloat(index, (Float)param);
			}
			else if (param instanceof Double) {
				ps.setDouble(index, (Double)param);
			}
			else if (param instanceof String) {
				ps.setString(index, param.toString());
			}
			else if (param instanceof java.util.Date) {
				ps.setDate(index, (java.sql.Date)param);
			}
			else if (param instanceof Boolean) {
				ps.setBoolean(index, (Boolean)param);
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.Write(e.getMessage());
		}
	}
}
